//
//  NSString+DSTools.m
//  Note
//
//  Created by DongSen on 2018/11/14.
//  Copyright © 2018年 董森森. All rights reserved.
//

#import "NSString+DSTools.h"

@implementation NSString (DSTools)

+ (BOOL)validateVerifyIDCard:(NSString *)code{
    code = [code stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    int length =0;
    if (!code) {
        return NO;
    }else {
        length = (int)code.length;
        
        if (length !=15 && length !=18) {
            return NO;
        }
    }
    // 省份代码
    NSArray *areasArray =@[@"11",@"12", @"13",@"14", @"15",@"21", @"22",@"23", @"31",@"32", @"33",@"34", @"35",@"36", @"37",@"41", @"42",@"43", @"44",@"45", @"46",@"50", @"51",@"52", @"53",@"54", @"61",@"62", @"63",@"64", @"65",@"71", @"81",@"82", @"91"];
    
    NSString *valueStart2 = [code substringToIndex:2];
    BOOL areaFlag =NO;
    for (NSString *areaCode in areasArray) {
        if ([areaCode isEqualToString:valueStart2]) {
            areaFlag =YES;
            break;
        }
    }
    
    if (!areaFlag) {
        return false;
    }
    
    
    NSRegularExpression *regularExpression;
    NSUInteger numberofMatch;
    
    int year =0;
    switch (length) {
        case 15:
            year = [code substringWithRange:NSMakeRange(6,2)].intValue +1900;
            
            if (year %4 ==0 || (year %100 ==0 && year %4 ==0)) {
                
                regularExpression = [[NSRegularExpression alloc] initWithPattern:@"^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$"
                                                                         options:NSRegularExpressionCaseInsensitive
                                                                           error:nil];//测试出生日期的合法性
            }else {
                regularExpression = [[NSRegularExpression alloc]initWithPattern:@"^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$"
                                                                        options:NSRegularExpressionCaseInsensitive
                                                                          error:nil];//测试出生日期的合法性
            }
            numberofMatch = [regularExpression numberOfMatchesInString:code
                                                               options:NSMatchingReportProgress
                                                                 range:NSMakeRange(0, code.length)];
            
            if(numberofMatch >0) {
                return YES;
            }else {
                return NO;
            }
        case 18:
            year = [code substringWithRange:NSMakeRange(6,4)].intValue;
            if (year %4 ==0 || (year %100 ==0 && year %4 ==0)) {
                
                regularExpression = [[NSRegularExpression alloc] initWithPattern:@"^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx]$"
                                                                         options:NSRegularExpressionCaseInsensitive
                                                                           error:nil];//测试出生日期的合法性
            }else {
                regularExpression = [[NSRegularExpression alloc] initWithPattern:@"^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]$"
                                                                         options:NSRegularExpressionCaseInsensitive
                                                                           error:nil];//测试出生日期的合法性
            }
            numberofMatch = [regularExpression numberOfMatchesInString:code
                                                               options:NSMatchingReportProgress
                                                                 range:NSMakeRange(0, code.length)];
            
            if(numberofMatch >0) {
                int S = ([code substringWithRange:NSMakeRange(0,1)].intValue + [code substringWithRange:NSMakeRange(10,1)].intValue) *7 + ([code substringWithRange:NSMakeRange(1,1)].intValue + [code substringWithRange:NSMakeRange(11,1)].intValue) *9 + ([code substringWithRange:NSMakeRange(2,1)].intValue + [code substringWithRange:NSMakeRange(12,1)].intValue) *10 + ([code substringWithRange:NSMakeRange(3,1)].intValue + [code substringWithRange:NSMakeRange(13,1)].intValue) *5 + ([code substringWithRange:NSMakeRange(4,1)].intValue + [code substringWithRange:NSMakeRange(14,1)].intValue) *8 + ([code substringWithRange:NSMakeRange(5,1)].intValue + [code substringWithRange:NSMakeRange(15,1)].intValue) *4 + ([code substringWithRange:NSMakeRange(6,1)].intValue + [code substringWithRange:NSMakeRange(16,1)].intValue) *2 + [code substringWithRange:NSMakeRange(7,1)].intValue *1 + [code substringWithRange:NSMakeRange(8,1)].intValue *6 + [code substringWithRange:NSMakeRange(9,1)].intValue *3;
                int Y = S %11;
                NSString *M =@"F";
                NSString *JYM =@"10X98765432";
                M = [JYM substringWithRange:NSMakeRange(Y,1)];// 判断校验位
                if ([M isEqualToString:[code substringWithRange:NSMakeRange(17,1)]]) {
                    return YES;// 检测ID的校验位
                }else {
                    return NO;
                }
            }else {
                return NO;
            }
        default:
            return NO;
    }
}
+ (BOOL)validateOrganization:(NSString *)code{
    if ([code rangeOfString:@"-"].location == NSNotFound){
        NSString * last  = [code substringFromIndex:code.length-1];
        NSString * replace = [NSString stringWithFormat:@"-%@",last];
        code = [code stringByReplacingOccurrencesOfString:last withString:replace];
    }
    BOOL result = NO;
    NSArray *ws = @[@"3",@"7",@"9",@"10",@"5",@"8",@"4",@"2"];
    NSArray *str = @[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",
                     @"9",@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",
                     @"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z"];
    NSString *regex = @"^([0-9A-Z]){8}-[0-9|X]$";
    NSPredicate * pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    if (![pred evaluateWithObject:code]) {
        return result = NO;
    }
    
    int sum = 0;
    for (int i = 0; i < 8; i++) {
        NSString *temp = [code substringWithRange:NSMakeRange(i,1)];
        NSUInteger index = [str indexOfObject:temp];
        sum += index * [ws[i] intValue];
    }
    int c9 = 11 - (sum % 11);
    NSString *sc9 = [NSString stringWithFormat:@"%d",c9];
    if (11 == c9) {
        sc9 = @"0";
    } else if (10 == c9){
        sc9 = @"X";
    }
    NSString *lastA = [code substringFromIndex:([code length]-1)];
    return [sc9 isEqualToString:lastA];
}

//统一社会信用代码验证
+ (BOOL)validateSocialCredit:(NSString *)code{
    if(code.length != 18){
        return NO;
    }
    NSString *scN = @"^([0-9ABCDEFGHJKLMNPQRTUWXY]{2})([0-9]{6})([0-9ABCDEFGHJKLMNPQRTUWXY]{9})([0-9Y])$";
    NSPredicate *regextestSCNum = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", scN];
    if (![regextestSCNum evaluateWithObject:code]) {
        return NO;
    }
    NSArray *ws = @[@1,@3,@9,@27,@19,@26,@16,@17,@20,@29,@25,@13,@8,@24,@10,@30,@28];
    NSDictionary *zmDic = @{@"A":@10,@"B":@11,@"C":@12,@"D":@13,@"E":@14,@"F":@15,@"G":@16,@"H":@17,@"J":@18,@"K":@19,@"L":@20,@"M":@21,@"N":@22,@"P":@23,@"Q":@24,@"R":@25,@"T":@26,@"U":@27,@"W":@28,@"X":@29,@"Y":@30};
    NSMutableArray *codeArr = [NSMutableArray array];
    NSMutableArray *codeArr2 = [NSMutableArray array];
    codeArr[0] = [code substringWithRange:NSMakeRange(0,code.length-1)];
    codeArr[1] = [code substringWithRange:NSMakeRange(code.length-1,1)];
    
    int sum = 0;
    for (int i = 0; i < [codeArr[0] length]; i++) {
        [codeArr2 addObject:[codeArr[0] substringWithRange:NSMakeRange(i, 1)]];
    }
    NSScanner* scan;
    int val;
    for (int j = 0; j < codeArr2.count; j++) {
        scan = [NSScanner scannerWithString:codeArr2[j]];
        if (![scan scanInt:&val] && ![scan isAtEnd]) {
            codeArr2[j] = zmDic[codeArr2[j]];
        }
    }
    for (int x = 0; x < codeArr2.count; x++) {
        sum += [ws[x] intValue]*[codeArr2[x] intValue];
    }
    int c18 = 31 - (sum % 31);
    if (c18<10){
        if(c18!=[codeArr[1] integerValue])return NO;
    }
    else{
        for (NSString *key in zmDic.allKeys) {
            if (zmDic[key]==[NSNumber numberWithInt:c18]) {
                if (![codeArr[1] isEqualToString:key]) {
                    return NO;
                }
            }
        }
    }
    return YES;
}
@end
